Intersection of two arrays I [Hash, Binary Search]¶
Time: O(M+N); Space: O(1); easy
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Notes:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Solution¶
If the given array is not sorted and the memory is unlimited:
elif the given array is already sorted:
if m << n or m >> n:
else:
- Space: O(1)
else: (the given array is not sorted and the memory is limited)
1. Hash¶
[11]:
import collections
class Solution1(object):
"""
Time: TIME:O(MAX(M,N)*Log(MAX(M,N)))
Space: SPACE:O(1)
"""
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
if len(nums1) > len(nums2):
return self.intersect(nums2, nums1)
lookup = collections.defaultdict(int)
for i in nums1:
lookup[i] += 1
res = []
for i in nums2:
if lookup[i] > 0:
res += i,
lookup[i] -= 1
return res
[12]:
s = Solution1()
nums1 = [1,2,2,1]
nums2 = [2,2]
assert s.intersect(nums1, nums2) == [2,2]
nums1 = [4,9,5]
nums2 = [9,4,9,8,4]
assert s.intersect(nums1, nums2) == [4,9] or [9,4]
2. Hash¶
[13]:
import collections
class Solution2(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
c = collections.Counter(nums1) & collections.Counter(nums2)
intersect = []
for i in c:
intersect.extend([i] * c[i])
return intersect
[14]:
s = Solution2()
nums1 = [1,2,2,1]
nums2 = [2,2]
assert s.intersect(nums1, nums2) == [2,2]
nums1 = [4,9,5]
nums2 = [9,4,9,8,4]
assert s.intersect(nums1, nums2) == [4,9] or [9,4]
3. Binary search solution¶
If the given array is already sorted, and the memory is limited, and (m << n or m >> n).
[17]:
class Solution3(object):
"""
Time: O(min(M,N)*Log(max(M,N)))
Space: O(1)
"""
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
if len(nums1) > len(nums2):
return self.intersect(nums2, nums1)
def binary_search(compare, nums, left, right, target):
while left < right:
mid = left + (right - left) // 2
if compare(nums[mid], target):
right = mid
else:
left = mid + 1
return left
nums1.sort(), nums2.sort() # Make sure it is sorted, doesn't count in time.
res = []
left = 0
for i in nums1:
left = binary_search(lambda x, y: x >= y, nums2, left, len(nums2), i)
if left != len(nums2) and nums2[left] == i:
res += i,
left += 1
return res
[18]:
s = Solution3()
nums1 = [1,2,2,1]
nums2 = [2,2]
assert s.intersect(nums1, nums2) == [2,2]
nums1 = [4,9,5]
nums2 = [9,4,9,8,4]
assert s.intersect(nums1, nums2) == [4,9] or [9,4]
4.Two pointers solution.¶
If the given array is already sorted, and the memory is limited or m ~ n.
[19]:
class Solution4(object):
"""
Time: O(M+N)
Space: O(1)
"""
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1.sort(), nums2.sort() # Make sure it is sorted, doesn't count in time.
res = []
it1, it2 = 0, 0
while it1 < len(nums1) and it2 < len(nums2):
if nums1[it1] < nums2[it2]:
it1 += 1
elif nums1[it1] > nums2[it2]:
it2 += 1
else:
res += nums1[it1],
it1 += 1
it2 += 1
return res
[20]:
s = Solution4()
nums1 = [1,2,2,1]
nums2 = [2,2]
assert s.intersect(nums1, nums2) == [2,2]
nums1 = [4,9,5]
nums2 = [9,4,9,8,4]
assert s.intersect(nums1, nums2) == [4,9] or [9,4]